<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="关键字" prop="searchParam">
        <el-input
            v-model="queryParams.searchParam"
            placeholder="请输入用户ID、手机号搜索"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源渠道" prop="source">
        <el-select
          v-model="queryParams.source"
          placeholder="来源渠道"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.channel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select
            v-model="queryParams.userType"
            placeholder="用户类型"
            clearable
            size="small"
            style="width: 240px"
        >
          <el-option
              v-for="dict in dict.type.app_user_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="用户状态" prop="userStatus">
        <el-select
            v-model="queryParams.userStatus"
            placeholder="用户状态"
            clearable
            size="small"
            style="width: 240px"
        >
          <el-option
              v-for="dict in dict.type.app_user_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="登录方式" prop="loginType">
        <el-select
          v-model="queryParams.loginType"
          placeholder="登录方式"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.login_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" prop="id" align="center" width="120" sortable />
      <el-table-column label="用户ID" align="center" prop="userId" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="用户名" align="center" prop="username" :show-overflow-tooltip="true" width="120" />
      <el-table-column label="身份核验" align="center" prop="phoneNum" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.phoneNum || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册日期" align="center" prop="registerDate" width="160"/>
      <el-table-column label="来源渠道" prop="source" align="center" width="100"/>
      <el-table-column label="用户类型" align="center" prop="userType" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.userType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会员时间" align="center" prop="memberDate" width="300">
        <template slot-scope="scope">
          <span>{{ scope.row.memberDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录方式" align="center" prop="loginType" width="120"/>
      <el-table-column label="总次数" align="center" prop="totalChangeFaceCount" width="120" sortable />
      <el-table-column label="已使用次数" align="center" prop="changeFaceNum" width="120" sortable>
        <template slot-scope="scope">
          <el-button type="text" @click="handleViewFaceDetail(scope.row)">{{ scope.row.changeFaceNum }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="剩余次数" align="center" prop="retainChangeFaceNum" width="120" sortable/>
      <el-table-column label="最后登录" align="center" prop="lastLoginDate" width="200"/>
      <el-table-column label="操作" align="center" width="200" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
              size="mini"
              type="text"
              style="color: #f00"
              @click="handleFreeze(scope.row)"
          >{{ scope.row.status === 1 ? '冻结' : '取消冻结' }}</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleCancel(scope.row)"
            style="color: #f00"
            v-if="scope.row.userTypeCode === 2"
          >取消会员</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #f00"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 换脸次数详情对话框 -->
    <el-dialog title="换脸次数" :visible.sync="open" width="50%" append-to-body :close-on-click-modal="false">
      <div>
        <el-row style="margin-bottom: 10px">
          <el-col :span="12">
            <span>用户ID：{{ content.userId }}</span>
          </el-col>
          <el-col :span="12">
            <span>用户名：{{ content.username }}</span>
          </el-col>
        </el-row>
        <el-row style="margin-bottom: 10px">
          <el-col :span="12">
            <span>视频换脸：{{ content.videoFaceNum }}</span>
          </el-col>
          <el-col :span="12">
            <span>图片换脸：{{ content.picFaceNum }}</span>
          </el-col>
        </el-row>
      </div>
      <div>
<!--        <el-tag-->
<!--          v-for="(item, index) in tagItems"-->
<!--          :key="item.label"-->
<!--          :effect="tagIndex === index ? 'dark' : 'plain'"-->
<!--          style="margin-right: 10px;margin-bottom: 20px;margin-top: 10px;"-->
<!--          @click="handleClick(index)">-->
<!--          {{ item.label }}-->
<!--        </el-tag>-->
        <el-tabs type="card">
          <el-tab-pane v-for="(item, index) in tagItems" :key="index" :label="item.label"></el-tab-pane>
        </el-tabs>
        <el-form inline :ref="changeParams" :model="changeParams" size="small">
          <el-form-item label="换脸结果" prop="changeFaceResult">
            <el-select v-model="changeParams.changeFaceResult" @change="handleChangeParams($event, 'changeFaceResult')">
              <el-option label="全部" :value="null"></el-option>
              <el-option label="换脸成功" :value="true"></el-option>
              <el-option label="换脸失败" :value="false"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否删除" prop="deleteTag">
            <el-select v-model="changeParams.deleteTag" @change="handleChangeParams($event, 'deleteTag')">
              <el-option label="全部" :value="null"></el-option>
              <el-option label="已删除" :value="true"></el-option>
              <el-option label="未删除" :value="false"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否单次收费" prop="single">
            <el-select v-model="changeParams.single" @change="handleChangeParams($event, 'single')">
              <el-option label="全部" :value="null"></el-option>
              <el-option label="是" :value="true"></el-option>
              <el-option label="否" :value="false"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div v-for="(row, index) in changeFaceData" :key="index">
          <h3>{{ row.time }}</h3>
          <div style="display: flex;flex-wrap: wrap;align-items: center;justify-items: flex-start;">
            <div v-for="(item2, index2) in row.resourceInfo" :key="index2" style="position: relative;width: 32%;margin: 0 10px 10px 0;background: #f5f5f5">
              <video :src="item2.url" v-if="item2.type === 1" controls style="width: 100%;height: 240px;"></video>
              <el-image :src="item2.url" v-if="item2.type === 2" fit="fill" style="width: 100%;height: 300px"></el-image>
              <el-button type="text" style="font-weight: bold;position: absolute;top: -10px;right: 0;color: #f00;font-size: 20px" @click="handleTemplateDelete(item2)">X</el-button>
              <div style="position: absolute;top: 0;left: 0;">
                <div style="background: #eee;margin-top: 5px;padding: 3px;" v-if="item2.deleteTag === 1">该作品已被用户删除</div>
                <div style="background: #f00;color: #fff;margin-top: 5px;padding: 3px;display: inline-block;" v-if="item2.status === 3">换脸失败</div>
                <div style="color: #fff;margin-top: 5px;" v-if="item2.status === 1">
                  <div style="background: orangered;padding: 3px;display: inline-block;">换脸中</div>
                </div>
              </div>
              <div style="padding: 5px;line-height: 20px;">
                <div v-if="item2.requestId">requestid：{{ item2.requestId }}</div>
                <div>上传时间：{{ item2.uploadTime }}</div>
                <div>完成时间：{{ item2.completeTime }}</div>
                <div>换脸耗时：{{ item2.useTime }}</div>
                <div v-if="item2.deleteTime">删除时间：{{ item2.deleteTime }}</div>
                <div v-if="item2.failReason">失败原因：{{ item2.failReason }}</div>
                <div>换脸接口：{{ item2.useApi }}</div>
                <div>消耗点数：{{ item2.usePoint }}</div>
                <div>是否单次收费：{{ item2.single ? '是' : '否' }}</div>
              </div>
            </div>
          </div>
        </div>
        <pagination
            v-show="changeTotal>0"
            :total="changeTotal"
            :page.sync="changeParams.pageNum"
            :limit.sync="changeParams.pageSize"
            :page-sizes="[9, 27, 45, 90]"
            :page-size="9"
            @pagination="getChangeFaceData"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  cancelVipUser,
  freezeAppUser,
  getAppUserList,
  getChannelList,
  getUserChangeFaceDetail,
  getUserChangeFaceList,
  removeAppUser,
  removeTemplateInfo
} from "@/api/shuiyin/shuiyin.js";

export default {
  name: "AppUser",
  dicts: ['app_user_type', 'login_type', 'channel', 'app_user_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchParam: undefined,
        loginType: undefined,
        source: undefined,
        userType: undefined,
        userStatus: undefined,
      },
      // 来源渠道
      channelData: [],
      form: {
        pageNum: 1,
        pageSize: 9,
        param: 1,
        userId: ''
      },
      // 是否显示弹出层
      open: false,
      content: {},
      tagItems: [
        {
          value: 1,
          label: '全部'
        },
        {
          value: 2,
          label: '视频'
        },
        {
          value: 3,
          label: '图片'
        }
      ],
      tagIndex: 0,
      // 用户换脸数据
      changeFaceData: [],
      // 用户换脸数量
      changeTotal: 0,
      // 用户换脸请求参数
      changeParams: {
        pageNum: 1,
        pageSize: 9,
        userId: '',
        param: 1,
        changeFaceResult: null,
        deleteTag: null,
        single: null
      }
    };
  },
  created() {
    this.getList();
    this.getChannelData();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      getAppUserList(this.queryParams).then(
        response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getChannelData() {
      getChannelList({ pageNum: 1, pageSize: 1000 }).then(response => {
        this.channelData = response.rows;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    // 查看换脸详情
    handleViewFaceDetail(row) {
      this.changeParams.userId = row.id;
      this.changeParams.pageNum = 1;
      this.changeParams.pageSize = 9;
      getUserChangeFaceDetail(this.changeParams.userId).then(response => {
        this.content = response.data;
        this.tagItems[1].label = `视频(${this.content.videoFaceNum})`;
        this.tagItems[2].label = `图片(${this.content.picFaceNum})`;
        this.getChangeFaceData();
        this.open = true;
      })
    },
    // 获取换脸详情记录
    getChangeFaceData() {
      getUserChangeFaceList(this.changeParams).then(response => {
        this.changeFaceData = response.rows;
        this.changeTotal = response.total;
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const userId = row.id;
      this.$router.push("/userDetail/user/" + userId);
    },
    /** 冻结和取消冻结操作 */
    handleFreeze(row) {
      const userId = row.id;
      freezeAppUser(userId).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
    /** 取消会员 */
    handleCancel(row) {
      const userId = row.id;
      cancelVipUser(userId).then(response => {
        this.$modal.msgSuccess("取消成功");
        this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + userIds + '"的数据项？').then(function() {
        return removeAppUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** tag点击操作 */
    handleClick(index) {
      this.tagIndex = index;
      this.changeParams.param = index + 1;
      this.getChangeFaceData();
    },
    handleChangeParams(evt, name) {
      this.changeParams[name] = evt;
      this.getChangeFaceData()
    },
    /** 模板删除操作 */
    handleTemplateDelete(row) {
      const id = row.recordId;
      this.$modal.confirm('是否确认删除所选的数据项？').then(function() {
        return removeTemplateInfo(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
